package icu.lookyousmileface.boot_admin.controller

import icu.lookyousmileface.boot_admin.pojo.User
import lombok.extern.slf4j.Slf4j
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.ui.set
import org.springframework.util.StringUtils
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping

import java.util.*
import javax.servlet.http.HttpSession

/**
 * @title: ViewController
 * @projectName boot_admin
 * @description: 用于处理用户登陆的Controller
 * @author starrysky
 * @date 2021/1/12:25 下午
 */
@Controller
class IndexController {
    /**
     * 主页的请求
     */
    @GetMapping(value = ["/", "/login"])
    fun indexPage() = "login";

    @PostMapping(value = ["/login"])
    fun login(user: User, session: HttpSession, model:Model): String {
        println("login执行")
        if (StringUtils.hasLength(user.userName) && "123456".equals(user.password)) {
            session.setAttribute("loginUser", user);
            //重定向，防止表单重复提交
            return "redirect:/main.html";
        } else {
            model.set("msg","账号密码错误");
            return "login";
        }

    }

    /**
     * 防止重复提交，接收/main.html请求
     * 拦截器已经拦截过了无需操作
     */
    @GetMapping(value = ["/main.html"])
    fun mainPage():String{
        println("mainPage执行")
        return "main";
    }
}